var oGameMusic=document.getElementById("gameMusic");

var oMain=document.getElementById("main");
var bg1 = document.getElementById("bg1");
var bg2 = document.getElementById("bg2");
// 传送带
		setInterval(mapScroll,80);
		function mapScroll(){
			bg1.style.left = bg1.offsetLeft + 2 + "px";
			bg2.style.left = bg2.offsetLeft + 2 + "px";
			if (bg1.offsetLeft >= 343) {
				bg1.style.left = "-343px";
			}
			if( bg2.offsetLeft >= 343){
				bg2.style.left = "-343px";
			}


		}
// 帧动画
var oDiv=document.getElementById('brid');
	var index=0;
	var birdtimer=setInterval(function(){
		oDiv.style.background="url(img/bird"+index+".png)";
		if (index==1) {
		 	index=0;
		}
		else{
			index=1;
		}
	},30);
	// logo上下晃动
var oLogo=document.getElementById("logo");
var s=3;
setInterval(function(){
	if(oLogo.offsetTop>150||oLogo.offsetTop<120){
		s=-s;
	}
oLogo.style.top=oLogo.offsetTop+s+"px";
},80)

// 开始按钮
var oStart=document.getElementById("start");
eventUtill.addEventHandler(oStart,"click",hidn);
function hidn(){
	//隐藏开始菜单
	oStart.style.display="none";
	oLogo.style.display="none";
	// js动态创建
	oBird=document.createElement("div");
	//创建小鸟
	oBird.className="oBird";
	oMain.appendChild(oBird);

	// 给战斗小鸟添加帧动画
	var index=0;
	var birdtimer=setInterval(function(){
		oBird.style.background="url(img/bird"+index+".png)";
		if (index==1) {
		 	index=0;
		}
		else{
			index=1;
		}
	},30);
	//添加点击开始改变小鸟
	var speedY1=5;
	setInterval(function(){
	eventUtill.addEventHandler(oMain,"click",dBack);
	function dBack(){
			clearInterval();
		speedY1=-8;
				//上升时改变背景图片
				// if (speedY1<=0) {
				// 		var down=setInterval(function(){
				// 		oBird.style.background="url(img/up_bird"+index+".png)";
				// 		if (index==1) {
				// 		 	index=0;
				// 		}
				// 		else{
				// 			index=1;
				// 		}
				// 			},13);
				// };
		
	}
	speedY1++;
			//下降改变图片
				// if (speedY1>0) {
				// 		var up=setInterval(function(){
				// 		oBird.style.background="url(img/down_bird"+index+".png)";
				// 		if (index==1) {
				// 		 	index=0;
				// 		}
				// 		else{
				// 			index=1;
				// 		}
				// 			},13);
				// };
		if (speedY1>10) {
			speedY1=10;
		};
	oBird.style.top=oBird.offsetTop+speedY1+"px";
	},30)
	//添加管道; 碰撞
		//上管道
		//var oUp;

		var createPipe=setInterval(function(){
		var randomHight=parseInt(Math.random()*150+60);
		var oUp=document.createElement("div");
		var oUp1=document.createElement("div");
		oUp.style.height=randomHight+"px";
		oUp.className="pipeUp";
		oUp1.style.background="url(img/up_pipe.png)";
		oUp1.style.width="62px";
		oUp1.className="pipeUp";
		oUp1.style.height="60px";
		oUp1.style.left="0px";
		oUp1.style.top=randomHight-60+"px";
		oUp.appendChild(oUp1);
		oMain.appendChild(oUp);

		//下管道创建
		var oDown=document.createElement("div");
		var oDown1=document.createElement("div");
		oDown.style.height=330-randomHight+"px";
		oDown.className="pipeDown";
		oDown1.style.background="url(img/down_pipe.png)";
		oDown1.style.width="62px";
		oDown1.className="pipeDown";
		oDown1.style.height="60px";
		oDown1.style.left="0px";
		oDown1.style.bottom=330-randomHight-60+"px";
		oDown.appendChild(oDown1);
		oMain.appendChild(oDown);
	
		},2000)
		
		//删除超出的管道 
		var timerpipe=setInterval(function(){
				var pipe=setInterval(function(){
				var Opipeup=document.getElementsByClassName('pipeUp')[0];
				var Opipedown=document.getElementsByClassName('pipeDown')[0];
				Opipeup.style.left=Opipeup.offsetLeft-4+"px";
				Opipedown.style.left=Opipeup.offsetLeft-4+"px";
				oUpLeft=parseInt(getStyle( Opipeup , "left" ) );
				oUpHeight=parseInt(getStyle( Opipeup , "height" ) );
				oDownLeft=parseInt(getStyle( Opipedown , "left" ) );
				oBirdLeft=parseInt(getStyle(oBird,"left") );
				oBirdWidth=parseInt(getStyle(oBird,"width") );
				oBirdTop=parseInt(getStyle(oBird,"top") );
				oBirdHeight=parseInt(getStyle(oBird,"height") );
				//判断是否发生碰撞
				if( (oBirdLeft+oBirdWidth>=Opipedown.offsetLeft&&oBirdTop>=oUpHeight+92)||(oBirdLeft+oBirdWidth>=Opipedown.offsetLeft&&oBirdTop<=oUpHeight) ){
					alert("撞了");
					clearInterval(pipe);
				}
				if (oUpLeft<=-62) {
					oMain.removeChild(Opipeup);
					oMain.removeChild(Opipedown);
					clearInterval(pipe);
				};
			},30)
		
		},2000);

//hidn结尾
}